Logo

LEGACY by Gabriel

HTML Course

Discover the fundamentals of HTML, the standard markup language for creating web pages.

This course covers the essential elements of HTML, including structure, tags, attributes, and best practices for web development.

Click here to gain access to our Coding Playground

HTML is the standard markup language for creating Web pages.

What is HTML?

  • HTML stands for Hyper Text Markup Language
  • HTML is the standard markup language for creating Web pages
  • HTML describes the structure of a Web page
  • HTML consists of a series of elements
  • HTML elements label pieces of content such as "this is a heading", "this is a paragraph", "this is a link", etc.

Images can improve the design and the appearance of a web page.

HTML Images

Example

  • <img src="pic_trulli.jpg" alt="Italian Trulli">
  • <img src="img_girl.jpg" alt="Girl in a jacket">
  • <img src="img_chania.jpg" alt="Flowers in Chania">

HTML Images Syntax

The HTML <img> tag is used to embed an image in a web page. Images are linked to web pages and the <img> tag creates a holding space for the referenced image.

The <img> tag is empty, it contains attributes only, and does not have a closing tag.

Required Attributes

  • src - Specifies the path to the image
  • alt - Specifies alternate text for the image

Syntax:

<img src="url" alt="alternatetext">

Examples

  • <img src="img_chania.jpg" alt="Flowers in Chania">
  • <img src="wrongname.gif" alt="Flowers in Chania"> (when image not found)

Image Size

  • <img src="img_girl.jpg" alt="Girl in a jacket" style="width:500px;height:600px;">
  • <img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">

Width and Height, or Style?

Both work, but using style is recommended:

  • <img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
  • <img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px;">

Other Concepts

  • Images in Sub-Folders: <img src="/images/html5.gif" alt="HTML5 Icon">
  • External Images: <img src="https://www.w3schools.com/images/w3schools_green.jpg" alt="W3Schools.com">
  • Animated Images: <img src="programming.gif" alt="Computer Man">
  • Image as a Link: <a href="default.asp">   <img src="smiley.gif" alt="HTML tutorial"> </a>
  • Image Floating: <img src="smiley.gif" style="float:right"> (right aligned) <img src="smiley.gif" style="float:left"> (left aligned)

Common Image Formats

  • APNG – .apng
  • GIF – .gif
  • ICO – .ico, .cur
  • JPEG – .jpg, .jpeg, .jfif, .pjpeg, .pjp
  • PNG – .png
  • SVG – .svg

Chapter Summary

  • Use <img> to define an image
  • Use src for the image path
  • Use alt for alternate text
  • Control size with width, height, or CSS
  • Use CSS float to align images

HTML Image Maps

With HTML image maps, you can create clickable areas on an image.

Image Maps

The HTML <map> tag defines an image map. An image map is an image with clickable areas, defined with one or more <area> tags.

Example:

  • <img src="workplace.jpg" alt="Workplace" usemap="#workmap">
  • <map name="workmap">
      <area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
      <area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
      <area shape="circle" coords="337,300,44" alt="Coffee" href="coffee.htm">
    </map>

How Does it Work?

The idea is that different actions can be performed depending on where you click in the image. The image is linked to the map using the usemap attribute:

<img src="workplace.jpg" alt="Workplace" usemap="#workmap">

Create Image Map

Then, use the <map> element with a name attribute matching the image’s usemap value:

<map name="workmap"> ... </map>

The <area> Element

Each clickable area is defined with an <area> element. You must set a shape and coords.

  • rect – rectangle
  • circle – circle
  • poly – polygon
  • default – the whole image

Shape Examples

Rectangle:

<area shape="rect" coords="34,44,270,350" href="computer.htm">

Circle:

<area shape="circle" coords="337,300,44" href="coffee.htm">

Polygon:

<area shape="poly" coords="140,121,181,116,204,160,204,222,191,270,140,329,85,355,58,352,37,322,40,259,103,161,128,147" href="croissant.htm">

Image Map and JavaScript

You can also trigger JavaScript with an onclick event:

<map name="workmap">
  <area shape="circle" coords="337,300,44" href="coffee.htm" onclick="myFunction()">
</map>

<script>
function myFunction() {
  alert("You clicked the coffee cup!");
}
</script>

Chapter Summary

  • Use the <map> element to define an image map
  • Use the <area> element for clickable regions
  • Link the image with usemap

HTML Image Tags

TagDescription
<img>Defines an image
<map>Defines an image map
<area>Defines a clickable area inside an image map
<picture>Container for multiple image resources

HTML Background Images

A background image can be specified for almost any HTML element.

Background Image on an HTML Element

Use the style attribute with background-image:

<p style="background-image: url('img_girl.jpg');"></p>
  

Background Image in <style> Element

<style>
p {
  background-image: url('img_girl.jpg');
}
</style>
  

Background Image on Entire Page

<style>
body {
  background-image: url('img_girl.jpg');
}
</style>
  

Background Repeat

By default, images repeat. Use background-repeat: no-repeat; to disable.

<style>
body {
  background-image: url('example_img_girl.jpg');
  background-repeat: no-repeat;
}
</style>
  

Background Cover

Use background-size: cover; to cover the entire element.

<style>
body {
  background-image: url('img_girl.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}
</style>
  

Background Stretch

Stretch to fit the element using 100% 100%:

<style>
body {
  background-image: url('img_girl.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: 100% 100%;
}
</style>